<?php
/**
 * @author     Olivier Parent
 * @copyright  Copyright (c) 2011 Artevelde University College Ghent
 */
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    # Elke methode in de Bootstrap die begint met '_init' wordt automatisch uitgevoerd.

    protected function _initResources()
    {
        // Maak _init() methodes voor resources in configs/application.ini
        $this->bootstrap('layout');    // Maakt een _initLayout()
        //$this->bootstrap('locale');    // Maakt een _initLocale()
        //$this->bootstrap('translate'); // Maakt een _initTranslate()
    }

    /**
     * Initialiseer jQuery View Helper
     */
    protected function _initJQueryViewHelper()
    {
        $view = $this->_getView();
        $view->addHelperPath('ZendX/JQuery/View/Helper', 'ZendX_JQuery_View_Helper');
        $view->jQuery()->setVersion('1.7')       // standaard 1.3.2
                       ->setUiVersion('1.8.16'); // standaard 1.7.1
    }

    /**
     * Initialiseer de Locale
     */
    protected function _initLocale()
    {
        try {
            $session = new Zend_Session_Namespace('language');
            if ( isset ($session->lang) ) {
                $locale = new Zend_Locale($session->lang);
            } else {
                $locale = new Zend_Locale();
            }
        } catch (Zend_Locale_Exception $e) {
            $locale = new Zend_Locale('en');
        }

        Zend_Registry::set('Zend_Locale', $locale);
    }

    /**
     * Initialiseer de vertalingen
     */
    protected function _initTranslate()
    {
        $translate = new Zend_Translate(
            array(
                'adapter' => 'array',
                'content' => APPLICATION_PATH . '/languages',
                'locale'  => Zend_Registry::get('Zend_Locale')->getLanguage(), // 'auto'
                'scan'    => Zend_Translate::LOCALE_DIRECTORY,
            )
        );

        Zend_Registry::set('Zend_Translate', $translate);
    }

    /**
     * Initialiseer de Navigation View Helper
     */
    protected function _initNavigationViewHelper()
    {
        /**
         * @todo Gebruik database i.p.v. XML.
         */
        $xml = APPLICATION_PATH . '/configs/navigation.xml';
        $pages = new Zend_Config_Xml($xml);
        $navigation = new Zend_Navigation($pages);
        $storage = Zend_Auth::getInstance()->getStorage()->read();
        $role = (isset ($storage->user_role) ) ?  $storage->user_role : 'guest';

        $view = $this->_getView();
        $view->navigation($navigation)
             //->setUseTranslator( Zend_Registry::get('Zend_Translate') )
             ->setAcl( new Ahs_Acl() )
             ->setRole($role);
        
        $lang = strtoupper( Zend_Registry::get('Zend_Locale')->getLanguage() );
        
        $xml = APPLICATION_PATH . '/configs/languages.xml';
        $languages = new Zend_Config_Xml($xml);
        
        $navigation_lang = new Zend_Navigation($languages);
        $navigation_lang->findBy('label', $lang)
                        ->setActive(true);
        
        Zend_Registry::set('navigation_lang',  $navigation_lang);        
    }

    /**
     * Initialiseer taal van de formuliervalidatie
     */
    protected function _initValidate()
    {
        try {
            $translator = new Zend_Translate(
                array(
                    'adapter' => 'array',
                    'content' => APPLICATION_PATH . '/../../../ZendFramework/resources/languages', // Volledige versie van Zend Framework!
                    /**
                     * @todo Taal instelbaar maken.
                     */
                    'locale'  => 'auto',
                    'scan' => Zend_Translate::LOCALE_DIRECTORY,
                )
            );
            Zend_Validate_Abstract::setDefaultTranslator($translator);
        } catch (Zend_Translate_Exception $e) {
            // Doe niets.
        }
    }

    /**
     * Initialiseer de View Helpers
     */
    protected function _initViewHelpers()
    {
        // Om $view->baseUrl() beschikbaar te maken in deze bootstrap
        $front = $this->getResource('frontController');
        $front->setRequest( new Zend_Controller_Request_Http() );

        $view = $this->_getView();
        $view->doctype('HTML5'); // http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.doctype
        $view->headMeta()
             ->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
        $view->headTitle('ZendFrameworkDemo')
             ->setDefaultAttachOrder('PREPEND')
             ->setSeparator(' ← ');

        $view->headLink()
             ->headlink(array(
            'rel' => 'shortcut icon',
            'href' => $view->baseUrl('/images/favicon.ico'))
        );
    }

    /**
     * @return Zend_View
     */
    protected function _getView()
    {
        $layout = $this->getResource('layout');
        $view = $layout->getView();

        return $view;
    }

}
